package com.buss.product.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.controller.BaseController;
import com.buss.core.dic.RecordStatus;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.utils.StringUtil;
import com.buss.product.model.Brand;
import com.buss.product.service.BrandService;

@Controller
@RequestMapping("/admin/brand")
public class BrandController extends BaseController {

	@Autowired
	private BrandService brandService;
	
	@Override
	@RequestMapping("/page")
	public ModelAndView listPage(String menuid) {
		ModelAndView view = new ModelAndView("brand/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}

	@Override
	@RequestMapping("/{type}")
	public ModelAndView editPage(@PathVariable String type, Long id) {
		ModelAndView view = new ModelAndView("/brand/edit");
		Brand brand = new Brand();
		if(EDIT_PAGE.equals(type)){			
			brand = brandService.loadBrand(id);
		}else{
			brand.setStatus(RecordStatus.normal);
		}			
		view.addObject("type", type);
		view.addObject("brand", brand);
		return view;
	}

	@Override
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) throws WebException {
		brandService.removeBrand(ids);
		return SysStaticParam.resultMessage("删除品牌信息成功!");
	}
	
	/***
	 * 新增品牌信息
	 * @param brand
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add( Brand brand,HttpServletRequest request){
		brand.setCompanyId(getCompanyId(request));
		brand = brandService.saveBrand(brand);
		Map<String, Object> map = SysStaticParam.resultMessage("新增品牌信息成功!");
		map.put("brand", brand);
		return map;
	}
	
	/**
	 * 修改品牌信息
	 * @param brand
	 * @param request
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String,Object> edit( Brand brand,HttpServletRequest request){
		brand.setCompanyId(getCompanyId(request));
		brandService.editBrand(brand);
		return SysStaticParam.resultMessage("修改品牌信息成功!");		
	}
	
	/**
	 * 获取品牌信息列表
	 * @param name
	 * @param categoryId
	 * @param orderField
	 * @param orderType
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(String name,Long categoryId,String pinyin,String orderField,String orderType,Pagination page,HttpServletRequest request){
		OrderBy orderBy = null;
		if(StringUtil.isNotEmpty(orderField)){
			orderBy = new OrderBy();
			orderBy.add(orderField, orderType);
		}
		ResultList<Brand> rs = brandService.queryBrand(getCompanyId(request),getBranchId(request).toString(), name, pinyin,categoryId, orderBy, page);
		Map<String, Object> map = SysStaticParam.resultMessage("获取品牌信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}

}
